<?php
/**
 * 商城店铺控制器
 * @author maniac
 */
namespace app\shop\controller;
use app\common\logic\Stores as StoresLogic;
use app\common\model\ShopType AS ShopTypeModel;
use app\common\model\Stores AS StoresModel;

class Stores extends Base
{

    /**
     * where
     */
    public function where(){
		$shop_id = $this->shop_id();
        $keywords=input("keywords",'');
        $is_flag=input("is_flag",-1);
        $where[]=[
            ['id','eq',$shop_id],
        ];

        if($keywords){
            $where[]=[
                ['name','like','%'.$keywords.'%']
            ];
        }

        if($is_flag>-1){
            $where[]=[
                ['is_flag','eq',$is_flag]
            ];
        }

        $this->assign('is_flag',$is_flag);
        $this->assign('keywords',$keywords);
        return $where;
    }

    /**
     * 自定义字段
     */
    public function getField(){
        return "*,photos photos_pics,'' username,'' status_txt,'' region";
    }

    /**
     * 搜索商户
     */
    public function search()
    {
        $name = input('post.name/s', '');
        if (!$name) {
            return j(1, '请输入名称');
        }
        $where = [
            ['name', 'like', '%'.$name.'%'],
        ];
        $data = StoresModel::field('id,name')->where($where)->order('id ASC')->select();
        return j(0, 'success', $data);
    }



    /**
     * 添加/编辑附加
     * @param array $info 要编辑的信息
     */
    public function addAttach($info)
    {

//        dump($info);die;
//        $categories = MerchantsCategory::field('id,parent_id,name,is_last')->order('id ASC')->select();
//        $categories = recursion_one_array($categories);
//
//        $diyHtml = \app\common\service\Admins::diyHtml($info);

        $type_list=ShopTypeModel::column('id,parent_id,name,"" sub_list','id');

        $t_arr=[];
        foreach($type_list as $k=>$v){
            if($v['parent_id']==0){
                $t_arr[]=$v;
            }
        }

        foreach($t_arr as &$v){
            $v['sub_list']=[];
            foreach($type_list as $k=>$tv){
                if($tv['parent_id']==$v['id']){
                    $v['sub_list'][]=$tv;
                }
            }
        }

        $cids=[];
        if($info['category_id']){
            $o_arr=explode(',',$info['category_id']);
            foreach ($o_arr as $k=>$vv){
                $cids[$vv]=$vv;
            }
        }

        $tids=[];
        if($info['type_id']){
            $sub_arr=explode(',',$info['type_id']);
            foreach ($sub_arr as $k=>$vv){
                $tids[$vv]=$vv;
            }
        }
        //分类
        $this->assign('t_arr',$t_arr);
        $this->assign('o_arr',$cids);
        $this->assign('sub_arr',$tids);
        
    }

    /**
     * 获取自定义字段
     */
    public function getAddField(){
        return "*,photos photos_pics,'' username,'' status_txt,hj_pic hj_pic_str,image_uri image_uri_str,idcard_id idcard_pics,pic pics";
    }


    /**
     * 审核管理
     */
    public function applylist(){
        // 当前页数
        $page   = input('get.page/d', 1);
        // 模型
        $model  = $this->getModel();
        // 查询条件
        $where=[];

        $where[]=[
            ['status','neq',1]
        ];

        // 排序条件
        $order  = $this->order();
        // 数据总数
        $count  = $model::where($where)->count();
        // 每页条数
        $limit  = $this->limit();
        // 分页条件
        $offset = $this->offset($page, $limit);
        //获取字段
        $field = $this->getField()?$this->getField():true;
        // 分页数据
        $data   = $model::field($field)->where($where)->order($order)->limit($offset)->select();
        // 模板
        $template = $this->indexTemplate();

        // 列表附加
        $this->indexAttach($data);

        return $this->fetch($template, [
            'page'  => $page,
            'count' => $count,
            'limit' => $limit,
            'data'  => $data,

        ]);
    }

    /**
     * 编辑审核
     */
    public function applyadd(){
        $id    = input('param.id/d', 0);
        // 模型
        $model = $this->getModel();

        $field=$this->getAddField();

        $info  = $model::field($field)->where('id', $id)->find();
        // 模板
        $template = $this->addTemplate();
        // 添加/编辑页面附加
        $this->addAttach($info);

        return $this->fetch($template, [
            'info' => $info,
            'redirect_uri'=>'/manage/stores/applylist',
        ]);
    }

    /**
     * 拒绝理由
     */
    public function refuse(){
        $id         =input('id');
        $refuse_txt =input("refuse_txt");

        $StoresLogic=new StoresLogic();

        $res= $StoresLogic->refuse(['id'=>$id,'refuse_txt'=>$refuse_txt]);

        return $res;
    }
}
